﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SmartSpace.WpfShell.ServiceRef;

namespace SmartSpace.WpfShell.Modules.Dynamic
{
    /// <summary>
    /// Interaction logic for ListTemplateView.xaml
    /// </summary>
    public partial class ListTemplateView : Page, ISsView
    {
        private Guid _workflowId { get; set; }
        private Guid _activityId { get; set; }

        public ListTemplateView()
        {
            InitializeComponent();
        }

        public void LoadArguments(Dictionary<string, string> args)
        {
            _workflowId = new Guid(args["workflowid"]);
            _activityId = new Guid(args["activityid"]);

            ServiceRef.SsWebService client = App.GetProxy();
            client.GetListMetadataCompleted += new GetListMetadataCompletedEventHandler(client_GetListMetadataCompleted);
            client.GetListMetadataAsync(_workflowId, _activityId);
        }

        void client_GetListMetadataCompleted(object sender, GetListMetadataCompletedEventArgs e)
        {
            if (e.Error==null && e.Result.IsSuccess)
            {
                foreach (FieldMetadata item in e.Result.Data.Fields)
                {
                    if (item.Name != "__Name")
                    {
                        DataGridTextColumn column = new DataGridTextColumn();
                        column.Header = item.Caption;
                        System.Windows.Data.Binding binding = new System.Windows.Data.Binding();
                        binding.Path = new PropertyPath("[" + item.Name + "]");
                        column.Binding = binding;
                        _ctlDataGrid.Columns.Add(column);
                    }
                }

                ServiceRef.SsWebService client = App.GetProxy();
                PagedResultOfDetailData detailData = client.GetListData(_workflowId, _activityId, 1, 0, 25);
                if (detailData.IsSuccess)
                {
                    _ctlDataGrid.ItemsSource = detailData.Data;
                }
            }
        }

        /// <summary>
        /// 过滤数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _ctlBtnSearch_Click(object sender, RoutedEventArgs e)
        {
            ServiceRef.SsWebService client = App.GetProxy();
            PagedResultOfDetailData detailData = client.GetListData(_workflowId, _activityId, 1, 0, 25);
            if (detailData.IsSuccess)
            {
                _ctlDataGrid.ItemsSource = detailData.Data;
            }
        }

        private void DataGridHyperlinkColumn_Click(object sender, RoutedEventArgs e)
        {
            DetailData data = (e.OriginalSource as Hyperlink).DataContext as DetailData;

            NavigationService.Navigate(new UpdateTemplateView(data.WorkflowId, data.ActivityId, data.Id));
        }
    }
}
